﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace ESOL.Controls
{
    public partial class EsolPicViewer : UserControl
    {
        private string sLocationFilePath = "";

        public EsolPicViewer()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        /// <summary>
        /// 设置图片适合要求的显示尺寸
        /// </summary>
        /// <param name="setPicture"></param>
        private void SetSuitPictureBox()
        {
            this.picBox.Dock = DockStyle.Fill;
            this.picBox.SizeMode = PictureBoxSizeMode.Zoom;
            int x, y, height, width, vmax, hmax;
            x = this.pan_picture.HorizontalScroll.LargeChange;
            y = this.pan_picture.VerticalScroll.LargeChange;
            height = this.pan_picture.Height;
            width = this.pan_picture.Width;
            vmax = pan_picture.HorizontalScroll.Maximum;
            hmax = pan_picture.VerticalScroll.Maximum;
            try
            {

                Image image = Image.FromFile(this.picBox.ImageLocation);
                if (image.Height <= height && image.Width <= width)
                {
                    this.picBox.SizeMode = PictureBoxSizeMode.CenterImage;
                    this.picBox.Dock = DockStyle.Fill;
                }
                else
                {
                    this.picBox.SizeMode = PictureBoxSizeMode.AutoSize;
                    this.picBox.Dock = DockStyle.None;
                    this.picBox.Location = new Point(1, 0);
                    //MessageBox.Show("v--"+vmax+"-"+x+" h-- "+hmax+"-"+y);
                    this.pan_picture.AutoScrollPosition = new Point((vmax - x) / 2, (hmax - y) / 2);//设置滚动条位置为中间位置
                }
            }
            catch (Exception)
            {

            }
            this.btn_fact.Enabled = false;
            this.btn_suit.Enabled = true;
        }

        /// <summary>
        /// 显示图片
        /// </summary>
        /// <param name="setPicture"></param>
        private void SetPictureBox(Boolean setPicture)
        {

            if (setPicture == true)
            {
                this.picBox.Dock = DockStyle.Fill;
                this.picBox.SizeMode = PictureBoxSizeMode.Zoom;
                this.btn_fact.Enabled = true;
                this.btn_suit.Enabled = false;
            }
            else
            {
                this.picBox.Dock = DockStyle.None;
                this.picBox.SizeMode = PictureBoxSizeMode.AutoSize;
                this.picBox.SizeMode = PictureBoxSizeMode.CenterImage;
                this.picBox.Dock = DockStyle.Fill;
                this.btn_fact.Enabled = false;
                this.btn_suit.Enabled = true;
            }

        }

        /// <summary>
        /// 获取所有当前打开图片文件夹中包含的所有符合条件的图片
        /// </summary>
        private void FillCombox()
        {
            //得到图片所在文件夹的路径
            string filePath = "";
            filePath = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.LastIndexOf("\\"));
            //打开目录，并将目录中所有.jpg.bmp.gif类型的图片放入FileInDir集合
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(filePath);
            System.IO.FileInfo[] fileInfo = dir.GetFiles();
            this.combox_FileInDir.Items.Clear();
            foreach (FileInfo fi in fileInfo)
            {
                if (fi.Extension == ".JPG" || fi.Extension == ".jpg" || fi.Extension == ".bmp" || fi.Extension == ".BMP" || fi.Extension == ".gif" || fi.Extension == ".GIF")
                {
                    this.combox_FileInDir.Items.Add(fi);
                }
            }
            string name = this.openFileDialog1.FileName;
            int last = name.LastIndexOf(@"\");
            this.combox_FileInDir.Text = name.Substring(last + 1, name.Length - last - 1);
        }

        private void LoadLocationPic()
        {

        }

        private void EsolPicViewer_Load(object sender, EventArgs e)
        {
            this.picBox.Dock = DockStyle.Fill;
        }

        private void tsmOpenFile_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string name = this.openFileDialog1.FileName;
                int last = name.LastIndexOf(@"\");
                this.picBox.ImageLocation = name;
                this.Text = name.Substring(last + 1, name.Length - last - 1) + " -- Windows图片查看器";
                sLocationFilePath = this.picBox.ImageLocation;
                Image image = Image.FromFile(name);
                int x, y;
                x = image.Width;
                y = image.Height;
                SetPictureBox(true);
                toolSSLbl_info.Text = "当前打开图片所在目录－－" + name.Substring(0, last) + "\\    图片大小－－宽:" + x + " 高:" + y;
                if (openFileDialog1.Filter[openFileDialog1.FilterIndex].ToString() == "j")
                {
                    this.btn_contrarotate.Enabled = false;
                    this.btn_deasil.Enabled = false;
                }
                else
                {
                    this.btn_contrarotate.Enabled = true;
                    this.btn_deasil.Enabled = true;
                }
                FillCombox();
            }
        }

        /// <summary>
        /// 设置为适合尺寸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_suit_Click(object sender, EventArgs e)
        {
            SetPictureBox(true);
        }

        /// <summary>
        /// 设置为实际尺寸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_fact_Click(object sender, EventArgs e)
        {
            SetSuitPictureBox(); 
            SetSuitPictureBox();
        }

        /// <summary>
        /// 逆时针旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_contrarotate_Click(object sender, EventArgs e)
        {
            try
            {
                Image image = Image.FromFile(sLocationFilePath);
                if (image.Height >= this.pan_picture.Height || image.Width >= this.pan_picture.Width)
                {
                    SetPictureBox(true);
                }
                else
                {
                    SetSuitPictureBox();
                }
                this.picBox.Image.RotateFlip(RotateFlipType.Rotate90FlipXY);
                this.picBox.Refresh();
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 顺时针旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_deasil_Click(object sender, EventArgs e)
        {
            try
            {
                Image image = Image.FromFile(sLocationFilePath);
                if (image.Height >= this.pan_picture.Height || image.Width >= this.pan_picture.Width)
                {
                    SetPictureBox(true);
                }
                else
                {
                    SetSuitPictureBox();
                }
                this.picBox.Image.RotateFlip(RotateFlipType.Rotate270FlipXY);
                this.picBox.Refresh();

            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 上一张
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_up_Click(object sender, EventArgs e)
        {
            if (this.combox_FileInDir.SelectedIndex > 0)
            {
                this.combox_FileInDir.Text = this.combox_FileInDir.Items[this.combox_FileInDir.SelectedIndex - 1].ToString();
            }
        }

        /// <summary>
        /// 下一张
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_down_Click(object sender, EventArgs e)
        {
            if (this.combox_FileInDir.SelectedIndex < this.combox_FileInDir.Items.Count - 1)
            {
                this.combox_FileInDir.Text = this.combox_FileInDir.Items[this.combox_FileInDir.SelectedIndex + 1].ToString();
            }
        }

        private void combox_FileInDir_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = this.openFileDialog1.FileName;
            int last = name.LastIndexOf(@"\");
            this.picBox.ImageLocation = name.Substring(0, last) + "\\" + this.combox_FileInDir.Text;
            this.Text = this.combox_FileInDir.Text + " -- Windows图片查看器";
            Image image;
            Boolean boolean;
            int x, y;
            try
            {
                image = Image.FromFile(this.picBox.ImageLocation);
                if (image.Height >= this.pan_picture.Height && image.Width >= this.pan_picture.Width)
                {
                    boolean = true;
                }
                else
                {
                    boolean = false;
                }
                SetPictureBox(boolean);
                x = image.Width;
                y = image.Height;
                toolSSLbl_info.Text = "当前打开图片所在目录－－" + name.Substring(0, last) + "\\    图片大小－－宽:" + x + " 高:" + y;
            }
            catch (Exception)
            {

            }
            if (openFileDialog1.Filter[openFileDialog1.FilterIndex].ToString() == "j")
            {
                this.btn_contrarotate.Enabled = false;
                this.btn_deasil.Enabled = false;
            }
            else
            {
                this.btn_contrarotate.Enabled = true;
                this.btn_deasil.Enabled = true;
            }


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            btn_down_Click(sender, e);
        }

        Boolean change = false;
        private void btn_change_Click(object sender, EventArgs e)
        {
            if (change == false)
            {
                timer1.Start();
                change = true;
                this.btn_change.Text = " 停 止 ";
            }
            else
            {
                timer1.Stop();
                change = false;
                this.btn_change.Text = "幻灯片显示";
            }
        }

        private void PicViewer_SizeChanged(object sender, EventArgs e)
        {
            if (this.btn_fact.Enabled == false)
            {
                SetSuitPictureBox();
            }
            else
            {

            }

        }

    }
}
